package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Stories;

public class GenericStoriesDAO extends AutoKeys implements
		org.jetnuke.dao.StoriesDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericStoriesDAO.class);

	public Stories retrieveByPK(int storyId) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryModel.selectByID"));
		p.setInt(1, storyId);

		ResultSet rs = p.executeQuery();
		Stories s = new Stories();

		if (rs.next()) {
			fillStoryFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillStoryFromResultSet(Stories s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("fillStoryFromResultSet(Stories, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setACOMM(rs.getInt("ACOMM"));
		s.setAID(rs.getInt("AID"));
		s.setALANGUAGE(rs.getString("ALANGUAGE"));
		s.setASSOCIATED(rs.getString("ASSOCIATED"));
		s.setBODYTEXT(rs.getString("BODYTEXT"));
		s.setCATID(rs.getInt("CATID"));
		s.setCOMMENTS(rs.getInt("COMMENTS"));
		s.setCOUNTER(rs.getInt("COUNTER"));
		s.setHASPOLL(rs.getInt("HASPOLL"));
		s.setHOMETEXT(rs.getString("HOMETEXT"));
		s.setIHOME(rs.getInt("IHOME"));
		s.setINFORMANT(rs.getInt("INFORMANT"));
		s.setNOTES(rs.getString("NOTES"));
		s.setPOLLID(rs.getInt("POLLID"));
		s.setRATINGS(rs.getInt("RATINGS"));
		s.setSCORE(rs.getInt("SCORE"));
		s.setSID(rs.getInt("SID"));
		s.setTIME(rs.getTimestamp("TIME1"));
		s.setTITLE(rs.getString("TITLE"));
		s.setTOPIC(rs.getInt("TOPIC"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillStoryFromResultSet(Stories, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#incCounter(int)
	 */
	public void incCounter(int storyId) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("incCounter(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryModel.incCounter"));
		p.setInt(1, storyId);

		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("incCounter(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#update(org.jetnuke.entities.Stories)
	 */
	public void update(Stories st) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(Stories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryModel.update"));

		p.setInt(1, st.getACOMM());
		p.setInt(2, st.getAID());
		p.setString(3, st.getALANGUAGE());
		p.setString(4, st.getASSOCIATED());
		p.setString(5, st.getBODYTEXT());
		p.setInt(6, st.getCATID());
		p.setInt(7, st.getCOMMENTS());
		p.setInt(8, st.getCOUNTER());
		p.setInt(9, st.getHASPOLL());
		p.setString(10, st.getHOMETEXT());
		p.setInt(11, st.getIHOME());
		p.setInt(12, st.getINFORMANT());
		p.setString(13, st.getNOTES());
		p.setInt(14, st.getPOLLID());
		p.setInt(15, st.getRATINGS());
		p.setInt(16, st.getSCORE());
		p.setTimestamp(17, new Timestamp(st.getTIME().getTime()));
		p.setString(18, st.getTITLE());
		p.setInt(19, st.getTOPIC());
		p.setInt(20, st.getSID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(Stories) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#incComments(int)
	 */
	public void incComments(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("incComments(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.incComments"));
		p.setInt(1, id);

		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("incComments(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getStoriesSidDesc(int)
	 */
	public List getStoriesByTopid_SidDesc(int topic, int limit)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByTopid_SidDesc(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		if (topic == 0) {
			p = JForumExecutionContext.getConnection().prepareStatement(
					SystemGlobals.getSql("StoryModel.getStoriesSidDesc"));
			p.setInt(1, limit);
		} else {
			p = JForumExecutionContext.getConnection().prepareStatement(
					SystemGlobals.getSql("StoryModel.getStoriesSidDesc1"));
			p.setInt(1, topic);
			p.setInt(2, limit);

		}

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByTopid_SidDesc(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Stories u = new Stories();
			fillStoryFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#addNew(org.jetnuke.entities.Stories)
	 */
	public int addNew(Stories story) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Stories) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this.getStatementForAutoKeys("StoryModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("StoriesModel.lastGeneratedStoryID"));
		initNewStory(story, p);

		int id = this.executeAutoKeysQuery(p);

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(Stories) - end"); //$NON-NLS-1$
		}
		return id;
	}

	/**
	 * @param story
	 * @param p
	 */
	private void initNewStory(Stories st, PreparedStatement p) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initNewStory(Stories, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setInt(1, st.getACOMM());
		p.setInt(2, st.getAID());
		p.setString(3, st.getALANGUAGE());
		p.setString(4, st.getASSOCIATED());
		p.setString(5, st.getBODYTEXT());
		p.setInt(6, st.getCATID());
		p.setInt(7, st.getCOMMENTS());
		p.setInt(8, st.getCOUNTER());
		p.setInt(9, st.getHASPOLL());
		p.setString(10, st.getHOMETEXT());
		p.setInt(11, st.getIHOME());
		p.setInt(12, st.getINFORMANT());
		p.setString(13, st.getNOTES());
		p.setInt(14, st.getPOLLID());
		p.setInt(15, st.getRATINGS());
		p.setInt(16, st.getSCORE());
		p.setTimestamp(17, new Timestamp(st.getTIME().getTime()));
		p.setString(18, st.getTITLE());
		p.setInt(19, st.getTOPIC());

		if (logger.isDebugEnabled()) {
			logger.debug("initNewStory(Stories, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#countAll()
	 */
	public int countAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryModel.countAll"));

		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countAll() - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getTopByTopic(int)
	 */
	public Stories getTopByTopic(int topic) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getTopByTopic(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.getTopByTopic"));
		p.setInt(1, topic);

		ResultSet rs = p.executeQuery();
		Stories s = new Stories();

		if (rs.next()) {
			fillStoryFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getTopByTopic(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getStoriesGreaterTime(java.util.Date)
	 */
	public List getStoriesGreaterTime(Date time) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesGreaterTime(Date) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryModel.getStoriesGreaterTime"));
		p.setTimestamp(1, new Timestamp(time.getTime()));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesGreaterTime(Date) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#selectOrderBySidDescLimit(int)
	 */
	public List selectOrderBySidDescLimit(int i) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderBySidDescLimit(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryModel.selectOrderBySidDescLimit"));
		p.setInt(1, i);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderBySidDescLimit(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getStoriesTimeDesc()
	 */
	public List getStoriesTimeDesc() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesTimeDesc() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.getStoriesTimeDesc"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesTimeDesc() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getByTopic(int)
	 */
	public List getByTopic(int topicid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getByTopic(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryModel.getByTopic"));
		p.setInt(1, topicid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getByTopic(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getByInformant(java.lang.String)
	 */
	public List getByInformant(String uname) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getByInformant(String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.getByInformant"));
		p.setString(1, uname);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getByInformant(String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc(java.lang.String,
	 *      int, int, java.lang.String)
	 */
	public List searchOrderTimeDesc(String author, int category, int topicid,
			String query) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("searchOrderTimeDesc(String, int, int, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc"));
		p.setString(1, author);
		p.setInt(2, category);
		p.setInt(3, topicid);
		p.setString(4, query);
		p.setString(5, query);
		p.setString(6, query);
		p.setString(7, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc(String, int, int, String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc2(java.lang.String,
	 *      int, java.lang.String)
	 */
	public List searchOrderTimeDesc2(String author, int category, String query)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc2(String, int, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc2"));
		p.setString(1, author);
		p.setInt(2, category);
		p.setString(3, query);
		p.setString(4, query);
		p.setString(5, query);
		p.setString(6, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc2(String, int, String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc3(java.lang.String,
	 *      int, java.lang.String)
	 */
	public List searchOrderTimeDesc3(String author, int topicid, String query)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc3(String, int, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc3"));
		p.setString(1, author);
		p.setInt(2, topicid);
		p.setString(3, query);
		p.setString(4, query);
		p.setString(5, query);
		p.setString(6, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc3(String, int, String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc4(java.lang.String,
	 *      java.lang.String)
	 */
	public List searchOrderTimeDesc4(String author, String query)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc4(String, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc4"));
		p.setString(1, author);
		p.setString(2, query);
		p.setString(3, query);
		p.setString(4, query);
		p.setString(5, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc4(String, String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc5(int, int,
	 *      java.lang.String)
	 */
	public List searchOrderTimeDesc5(int category, int topicid, String query)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc5(int, int, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc5"));
		p.setInt(1, category);
		p.setInt(2, topicid);
		p.setString(3, query);
		p.setString(4, query);
		p.setString(5, query);
		p.setString(6, query);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc5(int, int, String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc6(int,
	 *      java.lang.String)
	 */
	public List searchOrderTimeDesc6(int category, String query)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc6(int, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc6"));
		p.setInt(1, category);
		p.setString(2, query);
		p.setString(3, query);
		p.setString(4, query);
		p.setString(5, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc6(int, String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc7(int,
	 *      java.lang.String)
	 */
	public List searchOrderTimeDesc7(int topicid, String query)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc7(int, String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc7"));
		p.setInt(1, topicid);
		p.setString(2, query);
		p.setString(3, query);
		p.setString(4, query);
		p.setString(5, query);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc7(int, String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#searchOrderTimeDesc8(java.lang.String)
	 */
	public List searchOrderTimeDesc8(String query) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc8(String) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.searchOrderTimeDesc8"));
		p.setString(1, query);
		p.setString(2, query);
		p.setString(3, query);
		p.setString(4, query);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("searchOrderTimeDesc8(String) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#selectOrderByCounterDescLimit(int)
	 */
	public List selectOrderByCounterDescLimit(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderByCounterDescLimit(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryModel.selectOrderByCounterDescLimit"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderByCounterDescLimit(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#selectVotedOrderScoreDesc(int)
	 */
	public List selectVotedOrderScoreDesc(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectVotedOrderScoreDesc(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryModel.selectVotedOrderScoreDesc"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectVotedOrderScoreDesc(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#selectCommentedOrderCommentDesc(int)
	 */
	public List selectCommentedOrderCommentDesc(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectCommentedOrderCommentDesc(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryModel.selectCommentedOrderCommentDesc"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectCommentedOrderCommentDesc(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getStoriesInTime(java.util.Date,
	 *      java.util.Date)
	 */
	public List getStoriesInTime(Date time, Date time2) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesInTime(Date, Date) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.getStoriesInTime"));
		p.setTimestamp(1, new Timestamp(time.getTime()));
		p.setTimestamp(2, new Timestamp(time2.getTime()));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesInTime(Date, Date) - end"); //$NON-NLS-1$
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#deleteByID(int)
	 */
	public void deleteByID(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryModel.deleteByID"));
		p.setInt(1, id);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByID(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#countByCatID(int)
	 */
	public int countByCatID(int catid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("countByCatID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.countByCatID"));
		p.setInt(1, catid);
		ResultSet rs = p.executeQuery();
		rs.next();

		int r = rs.getInt(1);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("countByCatID(int) - end"); //$NON-NLS-1$
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#deleteByCatID(int)
	 */
	public void deleteByCatID(int catid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("deleteByCatID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("StoryModel.deleteByCatID"));
		p.setInt(1, catid);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("deleteByCatID(int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#moveCat(int, int)
	 */
	public void moveCat(int old_cat, int new_cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("moveCat(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("StoryModel.moveCat"));
		p.setInt(1, new_cat);
		p.setInt(2, old_cat);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("moveCat(int, int) - end"); //$NON-NLS-1$
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.StoriesModel#getStoriesByCategory_SidDesc(int,
	 *      int)
	 */
	public List getStoriesByCategory_SidDesc(int catid, int limit)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByCategory_SidDesc(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("StoryModel.getStoriesByCategory_SidDesc"));
		p.setInt(1, catid);
		p.setInt(2, limit);

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByCategory_SidDesc(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}
}
